package com.zbbt.sportslottery.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.text.TextUtils;

public class TimeUtil {

	public static final long OneMinute = 60 * 1000;

	public static final long OneHour = OneMinute * 60;

	public static final long OneDay = OneHour * 24;

	public static final long LongDay = OneHour * 48;

	public static final String DateFormat = "yyyy-MM-dd HH:mm:ss";
	public static final String DateMinuteFormat = "yyyy-MM-dd HH:mm";
	public static final String WeekFormat = "yyyy-MM-dd EEEE";
	public static final String DayHHMMFormat = "yyyy-MM-dd HH:mm";
	public static final String DayFormat = "yyyy-MM-dd";
	public static final String Day2Format = "yyyyMMdd";
	public static final String TimeFormat = "HH:mm:ss";
	public static final String MinuteFormat = "HH:mm";
	public static final String MonthFormat = "MM-dd";
	public static final String MonthMinuteFormat = "MM-dd HH:mm";

	public static String getTimeInterval(String time) {
		String name = "";
		long temp = NumberUtil.StringToLong(time);
		long interval = System.currentTimeMillis() - temp;
		if (interval > LongDay) {
			name = formatLongTimeForCustom(Long.parseLong(time), MonthMinuteFormat);
		} else if (interval > OneDay) {
			name = "24小时前";
		} else if (interval > OneHour) {
			long hour = interval / OneHour;
			name = hour + "小时前";
		} else if (interval > OneMinute) {
			long minute = interval / OneMinute;
			name = minute + "分钟前";
		} else {
			name = "刚刚";
		}
		return name;
	}

	/**
	 * 返回时间格式：12-28 19:28
	 */
	public static String formatLongTimeForCustom(long time, String format) {
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 返回时间格式：星期一
	 */
	public static String getOnlyWeek(long time) {
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
		return sdf.format(date);
	}

	/**
	 * 返回时间格式：2011-12-28
	 */
	public static String getTimeDate(long time) {
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}

	/**
	 * 返回时间格式：201203281122
	 */
	public static String getTimeAsNumber(long time) {
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
		return sdf.format(date);
	}

	/**
	 * 格式化日期
	 * 
	 * @return
	 */
	public static String formatDate(String dateStr, String style) {
		if (TextUtils.isEmpty(dateStr)) {
			return "";
		}
		try {
			SimpleDateFormat simpleDateormat = new SimpleDateFormat(style);
			Date date = simpleDateormat.parse(dateStr);
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd-MM");
			return sdf.format(date);
		} catch (ParseException e) {
			return "";
		}
	}

	/***
	 * 日期格式：yyyy-MM-dd HH:mm:ss 转成时间戳
	 */
	public static long getDateToTime(String time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		long t = 0;
		try {
			date = format.parse(time);
			t = date.getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			// System.out.println("确认转换数据没错");
			e.printStackTrace();
		}
		return t;
	}

	/**
	 * 根据一个日期，返回是星期几的字符串,2012-10-26 星期五
	 * 
	 * @param sdate
	 * @return
	 */
	public static String getWeek(String sdate) {
		DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		Calendar c = Calendar.getInstance();
		try {
			date = sdf.parse(sdate);
			c.setTime(date);
		} catch (ParseException e) {
			return "";
		}
		String result = new SimpleDateFormat("yyyy-MM-dd EEEE").format(c.getTime());
		return result;
	}

	public static String getyyMMddHHmm(String dateStr) {
		return customFormatDate(dateStr, "yyyy-MM-dd HH:mm:ss", "MM-dd HH:mm");
	}

	/**
	 * 自定义时间格式
	 * 
	 * @param dateStr
	 *            时间字符串；
	 * @param oldStyle
	 *            传进来的时间字符串的格式；
	 * @param newStyle
	 *            希望得到的时间格式
	 * @return
	 */
	public static String customFormatDate(String dateStr, String oldStyle, String newStyle) {
		if (TextUtils.isEmpty(dateStr)) {
			return "";
		}
		try {
			SimpleDateFormat simpleDateormat = new SimpleDateFormat(oldStyle);
			Date date = simpleDateormat.parse(dateStr);
			SimpleDateFormat sdf = new SimpleDateFormat(newStyle);
			return sdf.format(date);
		} catch (ParseException e) {
			LogUtil.defLog("TimeUtils-customFormatDate()");
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 返回时间格式：12-28 19:28
	 */
	public static String getLongtimeToShorttime(String time) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		Date d = null;
		try {
			d = sf.parse(time);
		} catch (ParseException e) {
			LogUtil.defLog("TimeUtils-getLongtimeToShorttime()");
			e.printStackTrace();
		}
		return sdf.format(d);
	}

	/**
	 * 将时分转成总共多少分钟，用于推动比较时间大小
	 * 
	 * @param time
	 * @return
	 */
	public static int getTimeForMilliscond(String time) {
		try {
			String[] times = time.split(":");
			if (times.length == 2) {
				int hour = Integer.parseInt(times[0]);
				int minute = Integer.parseInt(times[1]);
				return hour * 60 + minute;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static Date getStringTime(String time) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		try {
			d = sf.parse(time);
		} catch (ParseException e) {
			LogUtil.defLog("TimeUtils-getLongtimeToShorttime()");
			e.printStackTrace();
		}
		return d;
	}

	public static String getToday() {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
		String formatTime = format.format(new Date());
		return formatTime;
	}

	public static boolean isDayAfter(String day1, String day2, boolean flag) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date1 = dateFormat.parse(day1);
			Date date2 = dateFormat.parse(day2);
			if (flag) {
				return date1.getTime() > date2.getTime();
			} else {
				return date1.getTime() >= date2.getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
}
